<template>
	<div>
		<el-tabs v-model="activeName">
			<el-tab-pane v-for="(item, index) in tabsList" :key="index" :label="item.label" :name="item.value"> </el-tab-pane>

			<div class="content-card">
				<html-label title="基本信息"  v-if="activeName == 'BaseDetail'" >
					<el-button v-auth="[nowRowData.isOperate == 1, baseConfig.auth.edit]" type="primary" @click="handleOperate('edit')"> 信息编辑 </el-button>
				</html-label>

				<component
					v-if="!loading"
					class="pd10"
					:is="whickComm[activeName]"
					:soleKey="`dpServiceProvider-${nowData.creditCode}`"
					@refresh="handleRefresh"
					:nowRowData="nowData"
				></component>
			</div>
		</el-tabs>
	</div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import BaseDetail from './BaseDetail/index.vue'
import DataChangeLog from '@/views/datacenter/components/DataChangeLog/index.vue'
import { baseConfig } from '../config'
import { getAreaObj } from '@/utils/util'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
import bus from '@/utils/eventbus'
import { useCommonStore } from '@/stores/common'

const { setLoading } = useCommonStore()
const { onDelete } = useOperate(6)

type Props = {
	nowRowData?: any
	api?: any
}
const props = withDefaults(defineProps<Props>(), {})

const emit = defineEmits(['refresh', 'rechange'])
const loading = ref(false)

const whickComm = {
	BaseDetail,
	DataChangeLog,
}

const tabsList: any = [
	{ label: '基础信息', value: 'BaseDetail' },
	{ label: '信息编辑记录', value: 'DataChangeLog' },
]
const activeName = ref('BaseDetail')

let nowData = ref(props.nowRowData)
provide('nowRowData', nowData)
const getInitData = () => {
	loading.value = true
	setLoading()
	apiMod[props.api.detail](props.nowRowData.id)
		.then((res) => {
			loading.value = false
			setLoading()
			const area: any = getAreaObj(res.data, '', 'set')
			const registerArea: any = getAreaObj(res.data, 'register', 'set')
			nowData.value = {
				...(res.data || props.nowRowData),
				areasName: area.names,
				areas: area.areaIds,
				registerAreaNames: registerArea.names,
				registerArea: registerArea.areaIds,
			}
		})
		.catch(() => {
			loading.value = false
		})
}

const handleOperate = (type) => {
	if (type == 'delete') {
		onDelete(props.nowRowData[baseConfig.rowKey], baseConfig.api.delete, props.nowRowData.providerName).then(() => {
			emit('refresh')
		})
		return
	}
	emit('rechange', 'Rechange', props.nowRowData, '信息编辑数据')
}

bus.on('menu-operate', (res) => {
	if (res.pageName == baseConfig.pageName) {
		handleOperate(res.operate)
	}
})

const handleRefresh = () => {
	emit('refresh')
}

onMounted(() => {
	getInitData()
})
</script>

<style scoped></style>
